home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / squport.arc / UTR.C < prev   
Text File  |  1985-06-16  |  2KB  |  91 lines

  1. #include <stdio.h>
  2. #include "sqcom.h"
  3. #include "usq.h"
  4. #define ERROR -1
  5.  
  6. /* initialize decoding functions */
  7.  
  8. init_cr()
  9. {
  10.     repct = 0;
  11. }
  12.  
  13. init_huff()
  14. {
  15.     bpos = 99;    /* force initial read */
  16. }
  17.  
  18. /* Get bytes with decoding - this decodes repetition,
  19.  * calls getuhuff to decode file stream into byte
  20.  * level code with only repetition encoding.
  21.  *
  22.  * The code is simple passing through of bytes except
  23.  * that DLE is encoded as DLE-zero and other values
  24.  * repeated more than twice are encoded as value-DLE-count.
  25.  */
  26.  
  27. int
  28. getcr(ib)
  29. FILE *ib;
  30. {
  31.     int c;
  32.  
  33.     if(repct > 0) {
  34.         /* Expanding a repeated char */
  35.         --repct;
  36.         return value;
  37.     } else {
  38.         /* Nothing unusual */
  39.         if((c = getuhuff(ib)) != DLE) {
  40.             /* It's not the special delimiter */
  41.             value = c;
  42.             if(value == EOF)
  43.                 repct = LARGE;
  44.             return value;
  45.         } else {
  46.             /* Special token */
  47.             if((repct = getuhuff(ib)) == 0)
  48.                 /* DLE, zero represents DLE */
  49.                 return DLE;
  50.             else {
  51.                 /* Begin expanding repetition */
  52.                 repct -= 2;    /* 2nd time */
  53.                 return value;
  54.             }
  55.         }
  56.     }
  57. }
  58.  
  59. /* eject eject */
  60.  
  61. /* Decode file stream into a byte level code with only
  62.  * repetition encoding remaining.
  63.  */
  64.  
  65. int
  66. getuhuff(ib)
  67. FILE *ib;
  68. {
  69.     int i;
  70.     int bitval;
  71.  
  72.     /* Follow bit stream in tree to a leaf*/
  73.     i = 0;    /* Start at root of tree */
  74.     do {
  75.         if(++bpos > 7) {
  76.             if((curin = getc(ib)) == ERROR)
  77.                 return ERROR;
  78.             bpos = 0;
  79.             /* move a level deeper in tree */
  80.             i = dnode[i].children[1 & curin];
  81.         } else
  82.             i = dnode[i].children[1 & (curin >>= 1)];
  83.     } while(i >= 0);
  84.  
  85.     /* Decode fake node index to original data value */
  86.     i = -(i + 1);
  87.     /* Decode special endfile token to normal EOF */
  88.     i = (i == SPEOF) ? EOF : i;
  89.     return i;
  90. }
  91.